#include <common.h>
#include <asm/io.h>

#include <regs_adi.h>
#include "adi_hal_internal.h"
#include <asm/arch/sprd_reg.h>
#include <asm/arch/sprd_eic.h>
#include <sprd_battery.h>
#include "sprd_chg_helper.h"

#ifdef DEBUG
#define debugf(fmt, args...) do { printf("chg: %s(): ", __func__); printf(fmt, ##args); } while (0)
#else
#define debugf(fmt, args...)
#endif

void dio6833e_start_charge(int value)
{
	/* nothing to do for charge */
}

void dio6833e_stop_charge(int value)
{
	/* nothing to do for charge */
}

int sprd_charge_pd_control(bool enable)
{
	/* nothing to do for charge */
	return 0;
}

static void sprdchg_set_recharge(void)
{
	/* nothing to do for charge */
}


void sprdchg_common_cfg(void)
{
	/* nothing to do for charge */
}

static void dio6833e_ic_init(void)
{
	/* nothing to do for charge */
}
static void dio6833e_reset_timer(void)
{
	/* nothing to do for charge */
}

static int dio6833e_cmd(enum sprdchg_cmd cmd,int value)
{
	switch (cmd) {
		case CHG_SET_CURRENT:
			/* nothing to do for charge */
			break;
		default:
			break;
	}

	return 0;
}

static struct sprdchg_ic_operations dio6833e_chg_op ={
	.ic_init = dio6833e_ic_init,
	.chg_start = dio6833e_start_charge,
	.chg_stop = dio6833e_stop_charge,
	.timer_callback = dio6833e_reset_timer,
	.chg_cmd = dio6833e_cmd,
};

void dio6833e_chg_init(void)
{
	sprdchg_register_ops(&dio6833e_chg_op);
}

int sprdchg_charger_is_adapter(void)
{
	int ret = ADP_TYPE_UNKNOW;
	int charger_status;
	charger_status = sci_adi_read(ANA_REG_GLB_CHGR_STATUS)
	    & (BIT_CDP_INT | BIT_DCP_INT | BIT_SDP_INT);

	switch (charger_status) {
	case BIT_CDP_INT:
		ret = ADP_TYPE_CDP;
		break;
	case BIT_DCP_INT:
		ret = ADP_TYPE_DCP;
		break;
	case BIT_SDP_INT:
		ret = ADP_TYPE_SDP;
		break;
	default:
		break;
	}
	return ret;
}

int dio6833_batter_connect(void)
{
    int state;
	int vbus_state;
	int low_num = 0;
	int num = 0;

	sprd_eic_request(EIC_CHG_INT);
	sprd_eic_request(5);
	udelay(3000);

	vbus_state = sprd_eic_get(160);
	debugf("vbus state is %d\n", vbus_state);

	if(vbus_state > 0){
		while(num++ < 120){
			state = sprd_eic_get(5);

			if(state == 0)
				low_num++;

			mdelay(10);
		}
		debugf("low_num is %d\n", low_num);

		if(low_num == 120)
			return 1;
		else
			return low_num>0?0:1;
	}else{
		return 1;
	}
}


